Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

new-struct

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

new-struct

Structs inspired from Golang

  • 0.1.1
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

new-struct

A minimalistic class system designed for flexibility, functional programming. Inspired from Golang's Struct concept.

Motivation:

  • new-struct is small and simple. All it does is composing functions and objects.
  • It doesn't have new and this keywords. So, you'll never have to fix scopes.
  • You can do currying, partial programming with both new-struct and structs.

How does a struct look?

var Struct = require('new-struct')

var Animal = Struct({
  sleep: sleep,
  speak: speak
})

module.exports = Animal;

function sleep (animal) { console.log('zzzz'); }

function speak (animal, text) { 
  console.log('%s says %s', animal.name, text);
}

Check out Usage and examples for more info about it.

Install

$ npm install new-struct

Usage

A new struct is defined by an object of methods:

Struct = require('new-struct')

Animal = Struct({
  sleep: sleep,
  run: run,
  speak: speak
})

function sleep (animal) {
  console.log('zzz');
}

function speak (animal, sound) {
  console.log('%s says %s', animal.name, sound)
}

function run (animal) {
  console.log('%s is running', animal.name)
}

To create a an instance of the Animal struct, just call it with an object. this and new keywords are not needed, everything is just functions.

dongdong = Animal({ name: 'dongdong' })
blackbear = Animal({ name: 'blackbear' })

dongdong.name
// => 'dong dong'

dongdong.run()
// dongdong is running

blackbear.sleep()
// blackbear is sleeping

Factory Functions

It doesn't support constructors, but constructor-like factory functions are easy to implement:

function NewAnimal (name, age) {
  return Animal({ name: name, age: age })
}

Note that you can attach your constructor as a static method. So, you could have such a module:

Animal = Struct({
  New: New,
  run: run,
  speak: speak
})

module.exports = Animal;

function New (name, age) {
  return Animal({ name: name, age: age })
}

function speak (animal, sound) {
  console.log('%s says %s', animal.name, sound)
}

function run (animal) {
  console.log('%s is running', animal.name)
}

This will allow other modules requiring this have more flexibility:

Animal = require('./animal')

// You can either create using constructor:
Animal.New('dong dong', 13)

// Or calling the constructor itself:
Animal({ name: 'dong dong', age: 13 })

// You can also access the methods of Animal:
Animal.methods.run({ name: 'black bear' })
// will output: black bear is running

Mixing

You can create structs that mixes other ones:

Animal = require('./animal')

Cat = Struct(Animal, {
  meow: meow
})

function meow (cat) {
  Animal.methods.speak(cat, 'meooww')
}

Notice that each struct has a property called methods that keeps all the functions passed to it, including the ones derived from other structs.

See the tests and examples for more info, or create issues & send pull requests to improve the documentation.

Keywords

FAQs

Package last updated on 13 Jan 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc